package action;

import com.opensymphony.xwork2.ModelDriven;
import dao.BookDAO;
import dao.CategoryDAO;
import model.BookModel;
import model.CategoryModel;

import java.util.List;

/**
 * 书籍相关操作action类
 */
public class BookAction extends BaseAction implements ModelDriven<BookModel> {

    private BookModel model = new BookModel();

    private List<BookModel> bookList;
    private CategoryModel category;

    public CategoryModel getCategory() {
        return category;
    }

    public void setCategory(CategoryModel category) {
        this.category = category;
    }

    public List<BookModel> getBookList() {
        return bookList;
    }

    public void setBookList(List<BookModel> bookList) {
        this.bookList = bookList;
    }
    /**
     * 展示所有图书
     * @return
     */
    public String list(){
        BookDAO dao = new BookDAO();
        this.bookList = dao.findBookByQuery(this.getModel().getCategoryId(),this.getModel().getBookName());
        CategoryDAO cdao = new CategoryDAO();
        this.category = cdao.getCategoryByCategoryId(this.getModel().getCategoryId());
        if(category==null){
            category = new CategoryModel();
            category.setCategoryName("所有书籍");
        }
        return "list";
    }

    @Override
    public BookModel getModel() {
        return model;
    }

}
